﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace LocalizationTest.Tools
{
  public partial class DrawingPanel : UserControl
  {
    public Panel Panel
    {
      get { return panel; }
      set { panel = (DoubleBufferPanel)value; }
    }
    public string PanelID
    {
      get { return panelID; }
      set { 
        panelID = value;
        labelPanelID.Text = panelID;
      }
    }

    int storedWidth;
    int storedHeight;
    Point storedLocation;
    DockStyle storedDockStyle;

    public LinkLabel LinkLabelHide
    {
      get { return linkLabelHide; }
    }

    string panelID;
    int index;

    double zoom = 1.0;
    double angle = 0;

    public bool SaveBmp
    {
      get { return checkBoxRecordBmp.Checked; }
    }

    public bool SaveEMF
    {
      get { return checkBoxRecordEmf.Checked; }
    }

    public double Angle
    {
      get { return angle; }
    }

    public double Zoom
    {
      get { return zoom; }
    }

    public DrawingPanel(int index)
    {
      InitializeComponent();
      this.index = index;
    }

    public void ZoomIn()
    {
      zoom *= 1.1;
    }

    public void ZoomOut()
    {
      zoom /= 1.1;
    }

    public void ResetZoom()
    {
      zoom = 1.0;
    }

    public void Minimize(bool bringToFront)
    {
      linkLabelHide.Text = panelID;
      ConfigurationTool.Configuration.WriteConfigurationEntry(panelID, SaveStateToString());
      linkLabelHide.Dock = DockStyle.Left;
      storedWidth = panel.Width;
      storedHeight = panel.Height;
      storedLocation = panel.Location;
      storedDockStyle = panel.Dock;

      panel.Dock = DockStyle.None;
      panel.Width = 150;
      panel.Height = linkLabelHide.Height * 2;
      panel.Location = new Point(20, 30 + index * 30);
      checkBoxRecordBmp.Visible = false;
      checkBoxRecordEmf.Visible = false;
      linkLabelRotate.Visible = false;
      labelPanelID.Visible = false;
      if (bringToFront) (panel.Parent as DrawingPanelContainer).BringPanelToFront(panel);
    }

    public void Restore()
    {
      linkLabelHide.Dock = DockStyle.Right; 
      linkLabelHide.Text = "hide";
      panel.Width = storedWidth;
      panel.Height = storedHeight;
      panel.Location = storedLocation;
      panel.Dock = storedDockStyle;

      checkBoxRecordBmp.Visible = true;
      checkBoxRecordEmf.Visible = true;
      labelPanelID.Visible = true;
      linkLabelRotate.Visible = true;
      (panel.Parent as DrawingPanelContainer).BringPanelToFront(panel);
      ConfigurationTool.Configuration.WriteConfigurationEntry(panelID, SaveStateToString());
    }

    public string SaveStateToString()
    {
      return "" +
        angle + ";" +
        zoom + ";" +
        panel.Width + ";" +
        panel.Height + ";" +
        panel.Location.X + ";" +
        panel.Location.Y + ";" +
        panel.Dock + ";" +
        (linkLabelHide.Text != "hide");
    }

    public void RestoreFromString(string stateAsString)
    {
      try
      {
        string[] parts = stateAsString.Split(';');
        angle = double.Parse(parts[0]);
        zoom = double.Parse(parts[1]);
        panel.Width = int.Parse(parts[2]);
        panel.Height = int.Parse(parts[3]);
        int x = int.Parse(parts[4]);
        int y = int.Parse(parts[5]);
        panel.Location = new Point(x, y);
        string dockStyle = parts[6];
        switch (dockStyle)
        {
          case "None": panel.Dock = DockStyle.None; break;
          case "Bottom": panel.Dock = DockStyle.Bottom; break;
          case "Top": panel.Dock = DockStyle.Top; break;
          case "Left": panel.Dock = DockStyle.Left; break;
          case "Right": panel.Dock = DockStyle.Right; break;
          case "Fill": panel.Dock = DockStyle.Fill; break;
        }
        if (bool.Parse(parts[7])) Minimize(false);
      }
      catch
      {
        MessageBox.Show("May be drawing panels were not restored correctly.");
        zoom = 1.0;
        angle = 0.0;
        panel.Width = 150;
        panel.Height = 150;
      }
    }

    private void checkBoxRecordEmf_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void linkLabelHide_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      if (linkLabelHide.Text == "hide") Minimize(true);
      else Restore();
    }

    private void linkLabelRotate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      angle += 90.0;
      if (angle > 360) angle -= 360;
      this.Panel.Invalidate();
    }

  }
}
